// pages/index/filter.js
Page({
  /**
   * 页面的初始数据
   */
  data: {
    vehicleOption: ["不限", "轿车", "越野车/SUV", "商务车/MPV", "跑车", "皮卡", "面包车", "客车" , "7座", "房车", "敞篷车", "轿跑", "新能源"], //车辆类型选项
    vehicleType: "不限", //车辆选中的值
    priceValue: "不限", //价格
    priceOption: ["不限", "5万以下", "5-10万", "10-15万", "15-30万", "30万以上"], //价格选项
    sliderValue: 100, //价格slider百分比
    carAge: "不限", //车龄
    carAgeOption: ["不限", "0-1年", "1-3年", "3-5年", "5年"], //车龄选项
    carAgeSlider: 100, //车龄slider百分比
    displacementSlider: 100, //排量slider百分比
    mileageOption: ["不限", "0-2万公里", "2-5万公里", "5-10万公里", "10万公里以上"], //里程选项
    mileage: "不限", //里程
    mileageSlider: 100, //里程slider百分比
    countryOption: ["不限", "国产车", "德系车", "日系车", "欧系车", "法系车", "美系车", "韩系车"], //国别选项
    country: "不限", //国别
    carSourceLabelOption: ["不限", "新到", "降价", "门店", "报告"], //车源标签选项
    carSourceLabel: "不限", //车源
    gearboxOption: ["不限", "手动", "自动", "手自一体", "双离合", "CVT无级变速", "7档双离合"], //变速箱选项
    gearbox: "不限", //变速箱
    colorOption: ["不限", "黑色", "白色", "红色", "蓝色", "银色", "橙色", "紫色", "香橙金色", "灰色", "棕色", "绿色", "黄色", "其它"], //车身颜色
    color: "不限", //车身颜色

    //传值用字段
    _vehicleType: '', //车辆类型
    _price: '', //车辆价格
    _carAge: '', //车龄
    _carDisplacement: '', //汽车排量
    _showMileage: '', //汽车里程数
    _country: '', //国别
    _gearbox: '', //变速箱
    _color: '', //车辆颜色

  },
  onShow: function () {
    if (wx.getStorageSync('filterData') != '') {
      let filterData = wx.getStorageSync('filterData');
      this.setData({
        vehicleType: filterData.vehicleType, //车辆类型
        priceValue: filterData.priceValue, //车辆价格
        carAge: filterData.carAge, //车龄
        mileage: filterData.mileage, //汽车里程数
        country: filterData.country, //国别
        gearbox: filterData.gearbox, //变速箱
        color: filterData.color, //车辆颜色
        carAgeSlider: filterData.carAgeSlider, //车龄slider百分比
        displacementSlider: filterData.displacementSlider, //排量slider百分比
        sliderValue: filterData.sliderValue, //价格slider百分比
        mileageSlider: filterData.mileageSlider, //里程slider百分比
        _vehicleType: filterData._vehicleType, //车辆类型
        _price: filterData._price, //车辆价格
        _carAge: filterData._carAge, //车龄
        _carDisplacement: filterData._carDisplacement, //汽车排量
        _showMileage: filterData._showMileage, //汽车里程数
        _country: filterData._country, //国别
        _gearbox: filterData._gearbox, //变速箱
        _color: filterData._color, //车辆颜色
      })
    }
  },
  //改变选中的车辆类型
  changeVehicleType: function (e) {
    let vType = e.currentTarget.dataset.vehicletype;
    let type = '';
    switch (vType) {
      case '不限':
        type = '';
        break;
      case '轿车':
        type = 'car';
        break;
      case '越野车/SUV':
        type = 'SUV';
        break;
      case '商务车/MPV':
        type = 'MPV';
        break;
      case '跑车':
        type = 'Sports_car';
        break;
      case '皮卡':
        type = 'Pickup';
        break;
      case '面包车':
        type = 'van';
        break;
      case '客车':
        type = 'Passenger_car';
        break;
      case '货车':
        type = 'truck';
        break;
      case '7座':
        type = '7_seats';
        break;
      case '房车':
        type = 'Touring_car';
        break;
      case '敞篷车':
        type = 'open_car';
        break;
      case '轿跑':
        type = 'Coupe';
        break;
      case '新能源':
        type = 'new_energy';
        break;
    }
    this.setData({
      vehicleType: vType,
      _vehicleType: type
    })
  },
  //改变选中的价格
  priceEvent: function (e) {
    let priceText = e.currentTarget.dataset.price;
    let price = '';
    switch (priceText) {
      case '不限':
        price = '';
        break;
      case '5万以下':
        price = '0,5';
        break;
      case '5-10万':
        price = '5,10';
        break;
      case '10-15万':
        price = '10,15';
        break;
      case '15-30万':
        price = '15,30';
        break;
      case '30万以上':
        price = '30w_up';
        break;
    }
    this.setData({
      priceValue: priceText,
      _price: price,
      sliderValue: 100
    })
  },
  //改变选中的车龄
  carAgeEvent: function (e) {
    let carAgeText = e.currentTarget.dataset.carage;
    let carAge = '';
    switch (carAgeText) {
      case '不限':
        carAge = '';
        break;
      case '0-1年':
        carAge = '0,1';
        break;
      case '1-3年':
        carAge = '1,3';
        break;
      case '3-5年':
        carAge = '3,5';
        break;
      case '5年':
        carAge = '5_up';
        break;
    }
    this.setData({
      carAge: carAgeText,
      _carAge: carAge,
      carAgeSlider: 100
    })
  },
  //改变选中的里程
  mileageEvent: function (e) {
    let mileageText = e.currentTarget.dataset.mileage;
    let mileage = '';
    switch (mileageText) {
      case '不限':
        mileage = '';
        break;
      case '0-2万公里':
        mileage = '0,2';
        break;
      case '2-5万公里':
        mileage = '2,5';
        break;
      case '5-10万公里':
        mileage = '5,10';
        break;
      case '10万公里以上':
        mileage = '10w_up';
        break;
    }
    this.setData({
      mileage: mileageText,
      _showMileage: mileage,
      mileageSlider: 100
    })
  },

  //改变选中的国别
  countryEvent: function (e) {
    let countryText = e.currentTarget.dataset.country;
    let country = '';
    switch (countryText) {
      case '不限':
        country = '';
        break;
      case '国产车':
        country = 'china';
        break;
      case '德系车':
        country = 'german';
        break;
      case '日系车':
        country = 'japanese';
        break;
      case '欧系车':
        country = 'european';
        break;
      case '法系车':
        country = 'france';
        break;
      case '美系车':
        country = 'america';
        break;
      case '韩系车':
        country = 'korean';
        break;
    }
    this.setData({
      country: countryText,
      _country: country
    })
  },
  //改变选中的车源标签
  carSourceLabelEvent: function (e) {
    let carSourceLabel = e.currentTarget.dataset.carsourcelabel
    this.setData({
      carSourceLabel: carSourceLabel,
    })
  },
  //改变选中的变速箱
  gearboxEvent: function (e) {
    let gearboxText = e.currentTarget.dataset.gearbox;
    let gearbox = '';
    switch (gearboxText) {
      case '不限':
        gearbox = '';
        break;
      case '手动':
        gearbox = 'manual';
        break;
      case '自动':
        gearbox = 'manual';
        break;
      case '手自一体':
        gearbox = 'Hand_in_one';
        break;
      case '双离合':
        gearbox = 'Dual_clutch';
        break;
      case 'CVT无极变速':
        gearbox = 'CVT_continuously_variable';
        break;
      case '7档双离合':
        gearbox = 'Seven_speed_dual_clutch';
        break;
    }
    this.setData({
      gearbox: gearboxText,
      _gearbox: gearbox
    })
  },
  //改变选中的车身颜色
  colorEvent: function (e) {
    let colorText = e.currentTarget.dataset.color;
    let color = '';
    switch (colorText) {
      case '不限':
        color = '';
        break;
      case '黑色':
        color = 'black';
        break;
      case '白色':
        color = 'white';
        break;
      case '红色':
        color = 'red';
        break;
      case '蓝色':
        color = 'blue';
        break;
      case '蓝色':
        color = 'blue';
        break;
      case '银色':
        color = 'silver';
        break;
      case '橙色':
        color = 'orange';
        break;
      case '金色':
        color = 'golden';
        break;
      case '紫色':
        color = 'purple';
        break;
      case '香槟金色':
        color = 'Champagne_gold';
        break;
      case '灰色':
        color = 'grey';
        break;
      case '棕色':
        color = 'brown';
        break;
      case '绿色':
        color = 'green';
        break;
      case '黄色':
        color = 'yellow';
        break;
      case '其它':
        color = 'other';
        break;
    }
    this.setData({
      color: colorText,
      _color: color
    })
  },


  //自定义价格滚动条拖动方法
  changeCustomPrice: function (event) {
    if (0 <= event.detail.value && event.detail.value <= 20) {
      this.setData({
        _price: 0.5 * event.detail.value + ',' + '10',
        priceValue: "",
        sliderValue: event.detail.value,
      });
    } else if (20 < event.detail.value && event.detail.value <= 40) {
      this.setData({
        _price: 0.5 * event.detail.value + ',' + '20',
        priceValue: "",
        sliderValue: event.detail.value,
      });
    } else if (40 < event.detail.value && event.detail.value <= 60) {
      this.setData({
        _price: 0.5 * event.detail.value + ',' + '30',
        priceValue: "",
        sliderValue: event.detail.value,
      });
    } else if (60 < event.detail.value && event.detail.value <= 80) {
      this.setData({
        _price: 0.5 * event.detail.value + ',' + '40',
        priceValue: "",
        sliderValue: event.detail.value,
      });
    } else if (80 < event.detail.value && event.detail.value < 100) {
      this.setData({
        _price: 0.5 * event.detail.value + ',' + '99999',
        priceValue: "",
        sliderValue: event.detail.value,
      });
    } else if (event.detail.value == 100) {
      this.setData({
        _price: '',
        priceValue: "不限",
        sliderValue: 100,
      });
    }
  },
  //自定义车龄滚动条拖动方法
  changeCustomCarAge: function (event) {
    console.log(event.detail.value);
    if (event.detail.value != 100) {
      this.setData({
        carAge: '0-' + event.detail.value/10 + '年',
        _carAge: '0,'+event.detail.value/10,
        carAgeSlider: event.detail.value
      });
  } else {
      this.setData({
        carAge: "不限",
        _carAge: '',
        carAgeSlider: 100
      });
  }

    // if (0 <= event.detail.value && event.detail.value <= 20) {
    //   this.setData({
    //     carAge: '',
    //     _carAge: 0.1 * event.detail.value + ',' + '2',
    //     carAgeSlider: event.detail.value,
    //   });
    // } else if (20 < event.detail.value && event.detail.value <= 40) {
    //   this.setData({
    //     carAge: '',
    //     _carAge: 0.1 * event.detail.value + ',' + '4',
    //     carAgeSlider: event.detail.value,
    //   });
    // } else if (40 < event.detail.value && event.detail.value <= 60) {
    //   this.setData({
    //     carAge: '',
    //     _carAge: 0.1 * event.detail.value + ',' + '6',
    //     carAgeSlider: event.detail.value,
    //   });
    // } else if (60 < event.detail.value && event.detail.value <= 80) {
    //   this.setData({
    //     carAge: '',
    //     _carAge: 0.1 * event.detail.value + ',' + '8',
    //     carAgeSlider: event.detail.value,
    //   });
    // } else if (80 < event.detail.value && event.detail.value < 100) {
    //   this.setData({
    //     carAge: '',
    //     _carAge: 0.1 * event.detail.value + ',' + '99999',
    //     carAgeSlider: event.detail.value,
    //   });
    // } else if (event.detail.value == 100) {
    //   this.setData({
    //     carAge: "不限",
    //     _carAge: '',
    //     carAgeSlider: 100
    //   });
    // }
  },
  //自定义排量方法
  changeDisplacement: function (event) {
    if (event.detail.value == 100) {
      this.setData({
        _carDisplacement: '',
        carDisplacement: '不限',
        displacementSlider: event.detail.value
      });
    } else {
      this.setData({
        _carDisplacement: '0,' + (0.058 * event.detail.value).toFixed(1),
        carDisplacement: '排量 0-' + (0.058 * event.detail.value).toFixed(1),
        displacementSlider: event.detail.value
      });
    }
    // if (0 <= event.detail.value && event.detail.value <= 20) {
    //   this.setData({
    //     _carDisplacement: '0,' + 0.05 * event.detail.value,
    //     displacementSlider: event.detail.value
    //   });
    // } else if (20 < event.detail.value && event.detail.value <= 40) {
    //   this.setData({
    //     _carDisplacement: '0,' + 0.05 * event.detail.value,
    //     displacementSlider: event.detail.value
    //   });
    // } else if (40 < event.detail.value && event.detail.value <= 60) {
    //   this.setData({
    //     _carDisplacement: 0.05 * event.detail.value + ',' + '3',
    //     displacementSlider: event.detail.value
    //   });
    // } else if (60 < event.detail.value && event.detail.value <= 80) {
    //   this.setData({
    //     _carDisplacement: 0.05 * event.detail.value + ',' + '4',
    //     displacementSlider: event.detail.value
    //   });
    // } else if (80 < event.detail.value && event.detail.value < 100) {
    //   this.setData({
    //     _carDisplacement: 0.05 * event.detail.value + ',' + '99999',
    //     displacementSlider: event.detail.value
    //   });
    // } else if (event.detail.value == 100) {
    //   this.setData({
    //     _carDisplacement: '',
    //     displacementSlider: event.detail.value
    //   });
    // }
  },
  //自定义里程
  changeMileage: function (event) {
    if (event.detail.value == 100) {
      this.setData({
        _showMileage: '',
        mileage: "不限",
        mileageSlider: 100
      });
    } else {
      this.setData({
        _showMileage: '0,' +  event.detail.value/10,
        mileage: '0-' +  event.detail.value/10 + '万公里',
        mileageSlider: event.detail.value,
      });
    }

    // if (0 <= event.detail.value && event.detail.value <= 20) {
    //   this.setData({
    //     _showMileage: 0.15 * event.detail.value + ',' + '3',
    //     mileage: "",
    //     mileageSlider: event.detail.value,
    //   });
    // } else if (20 < event.detail.value && event.detail.value <= 40) {
    //   this.setData({
    //     _showMileage: 0.15 * event.detail.value + ',' + '6',
    //     mileage: "",
    //     mileageSlider: event.detail.value,
    //   });
    // } else if (40 < event.detail.value && event.detail.value <= 60) {
    //   this.setData({
    //     _showMileage: 0.15 * event.detail.value + ',' + '9',
    //     mileage: "",
    //     mileageSlider: event.detail.value,
    //   });
    // } else if (60 < event.detail.value && event.detail.value <= 80) {
    //   this.setData({
    //     _showMileage: 0.15 * event.detail.value + ',' + '12',
    //     mileage: "",
    //     mileageSlider: event.detail.value,
    //   });
    // } else if (80 < event.detail.value && event.detail.value < 100) {
    //   this.setData({
    //     _showMileage: 0.15 * event.detail.value + ',' + '99999',
    //     mileage: "",
    //     mileageSlider: event.detail.value,
    //   });
    // } else if (event.detail.value == 100) {
    //   this.setData({
    //     mileage: "不限",
    //     mileageSlider: 100
    //   });
    // }
  },
  //提交数据
  submit: function () {
    var pages = getCurrentPages(); //当前页面
    var prevPage = pages[pages.length - 2]; //上一页面
    // 给首页传值
    if (prevPage.route == "pages/index/index") {
      prevPage.setData({
        vehicleType: this.data._vehicleType,
        price: this.data._price,
        carAge: this.data._carAge,
        carDisplacement: this.data._carDisplacement,
        showMileage: this.data._showMileage,
        country: this.data._country,
        gearbox: this.data._gearbox,
        color: this.data._color,

        vehicleType2: this.data.vehicleType,
        price2: this.data.price,
        carAge2: this.data.carAge,
        carDisplacement2: this.data.carDisplacement,
        showMileage2: this.data.mileage,
        country2: this.data.country,
        gearbox2: this.data.gearbox,
        color2: this.data.color,
      })

      let filterData = {
        vehicleType: this.data.vehicleType, //车辆类型
        priceValue: this.data.priceValue == '' ? '' : this.data.priceValue, //车辆价格
        carAge: this.data.carAge == '' ? '' : this.data.carAge, //车龄
        mileage: this.data.mileage == '' ? '' : this.data.mileage, //汽车里程数
        country: this.data.country, //国别
        gearbox: this.data.gearbox, //变速箱
        color: this.data.color, //车辆颜色
        carAgeSlider: this.data.carAgeSlider, //车龄slider百分比
        displacementSlider: this.data.displacementSlider, //排量slider百分比
        sliderValue: this.data.sliderValue, //价格slider百分比
        mileageSlider: this.data.mileageSlider, //里程slider百分比

        //传值用字段
        _vehicleType: this.data._vehicleType, //车辆类型
        _price: this.data._price, //车辆价格
        _carAge: this.data._carAge, //车龄
        _carDisplacement: this.data._carDisplacement, //汽车排量
        _showMileage: this.data._showMileage, //汽车里程数
        _country: this.data._country, //国别
        _gearbox: this.data._gearbox, //变速箱
        _color: this.data._color, //车辆颜色
      }
      wx.setStorageSync('filterData', filterData)
    }
    wx.navigateBack({
      delta: 1
    })
  },
  //重置方法
  resetEvent: function (e) {
    var init = {
      vehicleType: "不限", //车辆选中的值
      priceValue: "不限", //价格
      carAge: "不限", //车龄
      mileage: "不限", //里程
      country: "不限", //国别
      carSourceLabel: "不限", //车源
      gearbox: "不限", //变速箱
      color: "不限", //车身颜色
      _vehicleType: '', //车辆类型
      _price: '', //车辆价格
      _carAge: '', //车龄
      _carDisplacement: '', //汽车排量
      _showMileage: '', //汽车里程数
      _country: '', //国别
      _gearbox: '', //变速箱
      _color: '', //车辆颜色
      carAgeSlider: 100, //车龄slider百分比
      displacementSlider: 100, //排量slider百分比
      sliderValue: 100, //价格slider百分比
      mileageSlider: 100, //里程slider百分比
    };
    this.setData(init)
    wx.setStorageSync('filterData', init)
  },
})